package com.ruoyi.web.controller.business;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.EDetail;
import com.ruoyi.business.domain.qo.QueryObjectEWallet;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.EWallet;
import com.ruoyi.business.service.IEWalletService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 钱包管理Controller
 *
 * @author hao
 * @date 2023-02-01
 */
@RestController
@RequestMapping("/business/wallet")
public class EWalletController extends BaseController {
    @Autowired
    private IEWalletService eWalletService;

    @Resource
    private SysUserMapper userMapper;

    /**
     * 查询钱包管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:wallet:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryObjectEWallet qo) {
        startPage();
        List<EWallet> list = eWalletService.selectEWalletList(qo);
        return getDataTable(list);
    }

    /**
     * 导出钱包管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:wallet:export')")
    @Log(title = "钱包管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EWallet eWallet) {
        List<EWallet> list = eWalletService.selectEWalletList(eWallet);
        ExcelUtil<EWallet> util = new ExcelUtil<EWallet>(EWallet.class);
        util.exportExcel(response, list, "钱包管理数据");
    }

    /**
     * 获取钱包管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:wallet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(eWalletService.getById(id));
    }

    /**
     * 新增钱包管理
     */
    @PreAuthorize("@ss.hasPermi('business:wallet:add')")
    @Log(title = "钱包管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/{userId}")
    public AjaxResult add(@PathVariable Long userId) {
        return toAjax(eWalletService.saves(userId));
    }

    /**
     * 修改钱包管理
     */
    @PreAuthorize("@ss.hasPermi('business:wallet:edit')")
    @Log(title = "钱包管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EWallet eWallet) {
        return toAjax(eWalletService.updateById(eWallet));
    }

    /**
     * 删除钱包管理
     */
    @PreAuthorize("@ss.hasPermi('business:wallet:remove')")
    @Log(title = "钱包管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        EWallet byId = eWalletService.getById(ids);
        if (byId.getCash() > 0) {
            throw new RuntimeException("不可删除");
        }
        return toAjax(eWalletService.removeById(ids));
    }

    /**
     * 冻结账户
     */
    @PostMapping("/dongjieWallet/{id}")
    public AjaxResult dongjie(@PathVariable Long id) {
        eWalletService.dongjieById(id);
        return AjaxResult.success();
    }

    /**
     * 用户列表展示
     */
    @GetMapping("/user/list")
    public AjaxResult userList() {
        List<EWallet> list = eWalletService.list();
        List<Long> ids = list.stream().map(EWallet::getUserId).collect(Collectors.toList());

        List<SysUser> sysUsers = userMapper.selectLists(ids);
        return AjaxResult.success(sysUsers);
    }

}
